﻿using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine;

public class ExtractorAB_ToSpriteAltals : MonoBehaviour {

    //input path
    string input_path = "E:/_pojie/updateRes/com.linecorp.LGSDG/files/Android/textureskilleffect/attack/";
    string out_path = "E:/_pojie/updateRes/com.linecorp.LGSDG/files/Android/textureskilleffect/attack/";
    string fileName;

    public void Extrator(string fileName)
    {
        this.fileName = fileName;
        var cur_output_path = out_path;

        var path = input_path + fileName;
        var assetBundle = AssetBundle.LoadFromFile(path);
        var tex_array = assetBundle.LoadAllAssets<Texture2D>();

        
        var tex = tex_array[0];
        UnityEngine.Debug.Log("tex.name" + tex.name);
        //https://musoucrow.github.io/2018/01/20/dnf_mobile_ex/
        //考虑readable问题
        var getTex = GetTexture(tex);
        UnityEngine.Debug.Log(" getTex.format " + getTex.format);

        int width = (int)getTex.width;
        int height = (int)getTex.height;
        var colors = getTex.GetPixels(0, 0, width, height);
        var newTex = new Texture2D(width, height, getTex.format, getTex.mipmapCount > 1);
        newTex.SetPixels(colors);
        newTex.Apply();
        var bytes = newTex.EncodeToPNG();
		//输出图片
        File.WriteAllBytes(cur_output_path + "/" + tex.name + ".png", bytes);

        
	
        List<string> lines = new List<string>();
        lines.Add(tex.name + ".png");
        var sprite_array = assetBundle.LoadAllAssets<Sprite>();
        foreach (Sprite sprite in sprite_array)
        {
            var spritename = "name" + ":" + sprite.name;
            var rect = "rect" + ":" + sprite.rect.x + ":" + sprite.rect.y + ":" + +sprite.rect.width + ":" + +sprite.rect.height;
            var pivot = "pivot" + ":" + sprite.pivot.x + ":" + sprite.pivot.y;
            lines.Add(spritename);
            lines.Add(rect);
            lines.Add(pivot);
        }
        var writeLines = lines.ToArray();
		//输出图集信息
        File.WriteAllLines(cur_output_path + "/" + tex.name + ".tpsheet", writeLines);

        assetBundle.Unload(true);
    }

    public Dictionary<Texture2D, Texture2D> texureMap = new Dictionary<Texture2D, Texture2D>();


    private Texture2D GetTexture(Texture2D tex)
    {
        if (!this.texureMap.ContainsKey(tex))
        {
            if (tex.width == 0 || tex.height == 0)
            {
                return null;
            }
            var copyTex = new Texture2D(tex.width, tex.height, tex.format, tex.mipmapCount > 1);
            copyTex.LoadRawTextureData(tex.GetRawTextureData());
            copyTex.Apply();
            var writeTex = new Texture2D(copyTex.width, copyTex.height);
            writeTex.SetPixels32(copyTex.GetPixels32());
            writeTex.Apply();
            writeTex.name = tex.name;
            this.texureMap[tex] = writeTex;
            Texture2D.DestroyImmediate(copyTex, true);
        }
        return this.texureMap[tex];
    }


    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
        {
            //Extrator("hover");
			//遍历目录 导出图集和图集信息
            var files = Directory.GetFiles(input_path);
            for (int i = 0; i < files.Length; i++)
            {
                UnityEngine.Debug.Log(Path.GetFileName(files[i]));
                Extrator(Path.GetFileName(files[i]));
            }
        }
    }
	
}
